package ui.swing.spinner.fmt;

import java.awt.BorderLayout;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

public class FormatSpinnerDemo extends JFrame {

	public FormatSpinnerDemo() {
		super("Spinner Demo (Formatted)");

		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
		p.setBorder(new EmptyBorder(10, 10, 10, 10));
		p.add(new JLabel("Dollar amount: "));

		SpinnerModel model = new SpinnerNumberModel(
				new Double(100.01),
				new Double(0), 
				null, 
				new Double(20)
		);
		JSpinner spn = new JSpinner(model);
		
		JFormattedTextField ftf = ((JSpinner.DefaultEditor)spn.getEditor()).getTextField();
		ftf.setColumns(10);
		NumberFormatter nf = new NumberFormatter(NumberFormat.getCurrencyInstance(Locale.CHINA));
		DefaultFormatterFactory dff = new DefaultFormatterFactory();
		dff.setDefaultFormatter(nf);
		dff.setDisplayFormatter(nf);
		dff.setEditFormatter(nf);
		ftf.setFormatterFactory(dff);
		
		p.add(spn);
		
		getContentPane().add(p, BorderLayout.NORTH);
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new FormatSpinnerDemo();
	}
	
}
